const sys = require(`./system.js`)
const $ = sys.$('助学账户信息')

const task = 'task:common'
const localStorage = 'localStorage-2'
sys.run($, task, (resList, cookie, index, username, password, item) => {
  return new Promise(async resolve => {
    // 请求接口
    const info = await $.publicJS.userInfo($, cookie)

    const boaInfo = await $.publicJS.boaInfo($, cookie)
    let couponsList = await $.publicJS.couponsList($, cookie)
    let summary = await $.publicJS.summary($, cookie)
    let transferList = await $.publicJS.transferList2($, cookie, 2)
    let amount1 = summary.items.totalAmount
    if(transferList.total) {
      amount1 += transferList.total.amount
    }

    // 构建 obj 信息
    resList.push({
      username: username,
      password: password,
      "序号": index + 1,
      "id": info.id,
      "微信昵称": info.userWechatNickName,
      "账户余额": info.accountUsableAmount,

      '可用盲盒': boaInfo.myChance,
      '可用优惠卷': couponsList.length,
      couponsList: couponsList.map(it => `${it.useCondition} - ${it.amount}`),
      '已购买金额': amount1
    })
    resolve(resList)
  })
}, (resList) => {
  console.log('任务结束 resList:', resList)
  if (resList && resList.length > 0) {
    resList = resList.sort((a1, a2) => {
      return a1['序号'] - a2['序号']
    })
    // 存储列表
    $.localStorage.set(task, resList, localStorage)
  }
}, 1200)
